package com.murphy.system.controller;


import com.google.gson.GsonBuilder;
import com.murphy.system.model.Menu;
import com.murphy.system.service.MenuService;
import com.murphy.utils.LoginAttribute;
import com.murphy.utils.PageController;
import com.murphy.utils.PageDataMaker;
import com.murphy.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @Author Murphy
 * @Date 2018-01-10 11:05
 */
@Controller
@RequestMapping("/Menu")
public class MenuController {

    private Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private MenuService menuService;

    @RequestMapping("list")
    public ModelAndView list(@ModelAttribute("menuListForm") PageController pageController, HttpServletRequest request){
        Map<String,Object> map = new HashMap<String, Object>();
        pageController.setSrchField(map);
        pageController.loadPage(new PageDataMaker() {
            @Override
            public List<?> getPageList(int startRow, int countPerPage, String orderColumn, String orderType, Map<String, Object> srchMap) {
                return menuService.getMenuList(srchMap,startRow,countPerPage);
            }
            @Override
            public int getTotalCount(Map<String, Object> srchMap) {
                return menuService.getMenuCount(srchMap);
            }
        });
        request.setAttribute("menuList",pageController.getPageList());
        request.setAttribute("count",pageController.getTotalCount());
        return new ModelAndView("/jsp/system/menu-list.jsp");
    }

    @RequestMapping("save")
    public ModelAndView save(@ModelAttribute("menuForm")Menu menu,HttpServletRequest request){
        try{
            String userId = request.getSession().getAttribute(LoginAttribute.SESSION_USER_ID).toString();
            menu.setCreateDate(new Date());
            menu.setCreateUser(userId);

            String level = request.getParameter("level");
            logger.debug("level:"+level);
            if("0".equals(level)){
                menu.setType(Menu.MODULE);
            }else {
                menu.setType(Menu.MENU);
            }

            String id = menuService.save(menu);
            logger.debug("saveMenu:"+id);
        }catch (Exception e){
            e.printStackTrace();
        }

        return new ModelAndView("/Menu/list");
    }

    public ModelAndView reUpdate(){
        return new ModelAndView("");
    }

    @RequestMapping("loadMenuList")
    public void loadMenuList(HttpServletRequest request, HttpServletResponse response){
        String result = "";
        try{
            List<Menu> list = menuService.getMenuList(null,false);
            result = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        StringUtils.outputString(request,response,result);
    }

}
